using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace MyCityCab.Core
{
	/// <summary>
	/// Strongly-typed collection for the Comment class.
	/// </summary>
	[Serializable]
	public partial class CommentCollection : ActiveList<Comment, CommentCollection> 
	{	   
		public CommentCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Comment table.
	/// </summary>
	[Serializable]
	public partial class Comment : ActiveRecord<Comment>
	{
		#region .ctors and Default Settings
		
		public Comment()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Comment(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Comment(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Comment(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Comment", TableType.Table, DataService.GetInstance("MyCityCab"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "Id";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = false;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarTitle = new TableSchema.TableColumn(schema);
				colvarTitle.ColumnName = "Title";
				colvarTitle.DataType = DbType.String;
				colvarTitle.MaxLength = 50;
				colvarTitle.AutoIncrement = false;
				colvarTitle.IsNullable = true;
				colvarTitle.IsPrimaryKey = false;
				colvarTitle.IsForeignKey = false;
				colvarTitle.IsReadOnly = false;
				colvarTitle.DefaultSetting = @"";
				colvarTitle.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTitle);
				
				TableSchema.TableColumn colvarCommentText = new TableSchema.TableColumn(schema);
				colvarCommentText.ColumnName = "CommentText";
				colvarCommentText.DataType = DbType.String;
				colvarCommentText.MaxLength = 1073741823;
				colvarCommentText.AutoIncrement = false;
				colvarCommentText.IsNullable = false;
				colvarCommentText.IsPrimaryKey = false;
				colvarCommentText.IsForeignKey = false;
				colvarCommentText.IsReadOnly = false;
				colvarCommentText.DefaultSetting = @"";
				colvarCommentText.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCommentText);
				
				TableSchema.TableColumn colvarUserId = new TableSchema.TableColumn(schema);
				colvarUserId.ColumnName = "User_Id";
				colvarUserId.DataType = DbType.Int32;
				colvarUserId.MaxLength = 0;
				colvarUserId.AutoIncrement = false;
				colvarUserId.IsNullable = false;
				colvarUserId.IsPrimaryKey = false;
				colvarUserId.IsForeignKey = true;
				colvarUserId.IsReadOnly = false;
				colvarUserId.DefaultSetting = @"";
				
					colvarUserId.ForeignKeyTableName = "User";
				schema.Columns.Add(colvarUserId);
				
				TableSchema.TableColumn colvarReplyToCommentId = new TableSchema.TableColumn(schema);
				colvarReplyToCommentId.ColumnName = "ReplyToComment_Id";
				colvarReplyToCommentId.DataType = DbType.Int32;
				colvarReplyToCommentId.MaxLength = 0;
				colvarReplyToCommentId.AutoIncrement = false;
				colvarReplyToCommentId.IsNullable = true;
				colvarReplyToCommentId.IsPrimaryKey = false;
				colvarReplyToCommentId.IsForeignKey = true;
				colvarReplyToCommentId.IsReadOnly = false;
				colvarReplyToCommentId.DefaultSetting = @"";
				
					colvarReplyToCommentId.ForeignKeyTableName = "Comment";
				schema.Columns.Add(colvarReplyToCommentId);
				
				TableSchema.TableColumn colvarCommentDate = new TableSchema.TableColumn(schema);
				colvarCommentDate.ColumnName = "CommentDate";
				colvarCommentDate.DataType = DbType.DateTime;
				colvarCommentDate.MaxLength = 0;
				colvarCommentDate.AutoIncrement = false;
				colvarCommentDate.IsNullable = false;
				colvarCommentDate.IsPrimaryKey = false;
				colvarCommentDate.IsForeignKey = false;
				colvarCommentDate.IsReadOnly = false;
				
						colvarCommentDate.DefaultSetting = @"(getdate())";
				colvarCommentDate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCommentDate);
				
				TableSchema.TableColumn colvarRate = new TableSchema.TableColumn(schema);
				colvarRate.ColumnName = "Rate";
				colvarRate.DataType = DbType.Decimal;
				colvarRate.MaxLength = 0;
				colvarRate.AutoIncrement = false;
				colvarRate.IsNullable = false;
				colvarRate.IsPrimaryKey = false;
				colvarRate.IsForeignKey = false;
				colvarRate.IsReadOnly = false;
				
						colvarRate.DefaultSetting = @"((0))";
				colvarRate.ForeignKeyTableName = "";
				schema.Columns.Add(colvarRate);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MyCityCab"].AddSchema("Comment",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("Id"); }

			set { SetColumnValue("Id", value); }

		}

		  
		[XmlAttribute("Title")]
		public string Title 
		{
			get { return GetColumnValue<string>("Title"); }

			set { SetColumnValue("Title", value); }

		}

		  
		[XmlAttribute("CommentText")]
		public string CommentText 
		{
			get { return GetColumnValue<string>("CommentText"); }

			set { SetColumnValue("CommentText", value); }

		}

		  
		[XmlAttribute("UserId")]
		public int UserId 
		{
			get { return GetColumnValue<int>("User_Id"); }

			set { SetColumnValue("User_Id", value); }

		}

		  
		[XmlAttribute("ReplyToCommentId")]
		public int? ReplyToCommentId 
		{
			get { return GetColumnValue<int?>("ReplyToComment_Id"); }

			set { SetColumnValue("ReplyToComment_Id", value); }

		}

		  
		[XmlAttribute("CommentDate")]
		public DateTime CommentDate 
		{
			get { return GetColumnValue<DateTime>("CommentDate"); }

			set { SetColumnValue("CommentDate", value); }

		}

		  
		[XmlAttribute("Rate")]
		public decimal Rate 
		{
			get { return GetColumnValue<decimal>("Rate"); }

			set { SetColumnValue("Rate", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public MyCityCab.Core.CommentCollection ChildCommentRecords()
		{
			return new MyCityCab.Core.CommentCollection().Where(Comment.Columns.ReplyToCommentId, Id).Load();
		}

		public MyCityCab.Core.JourneyCommentCollection JourneyCommentRecords()
		{
			return new MyCityCab.Core.JourneyCommentCollection().Where(JourneyComment.Columns.CommentId, Id).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a Comment ActiveRecord object related to this Comment
		/// 
		/// </summary>
		public MyCityCab.Core.Comment ParentComment
		{
			get { return MyCityCab.Core.Comment.FetchByID(this.ReplyToCommentId); }

			set { SetColumnValue("ReplyToComment_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a User ActiveRecord object related to this Comment
		/// 
		/// </summary>
		public MyCityCab.Core.User User
		{
			get { return MyCityCab.Core.User.FetchByID(this.UserId); }

			set { SetColumnValue("User_Id", value.Id); }

		}

		
		
		#endregion
		
		
		
		#region Many To Many Helpers
		
		 
		public MyCityCab.Core.JourneyCollection GetJourneyCollection() { return Comment.GetJourneyCollection(this.Id); }

		public static MyCityCab.Core.JourneyCollection GetJourneyCollection(int varId)
		{
			SubSonic.QueryCommand cmd = new SubSonic.QueryCommand(
				"SELECT * FROM Journey INNER JOIN JourneyComment ON "+
				"Journey.Id=JourneyComment.Journey_Id WHERE JourneyComment.Comment_Id=@Comment_Id", Comment.Schema.Provider.Name);
			
			cmd.AddParameter("@Comment_Id", varId, DbType.Int32);
			IDataReader rdr = SubSonic.DataService.GetReader(cmd);
			JourneyCollection coll = new JourneyCollection();
			coll.LoadAndCloseReader(rdr);
			return coll;
		}

		
		public static void SaveJourneyMap(int varId, JourneyCollection items)
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Comment_Id=@Comment_Id", Comment.Schema.Provider.Name);
			cmdDel.AddParameter("@Comment_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (Journey item in items)
			{
				JourneyComment varJourneyComment = new JourneyComment();
				varJourneyComment.SetColumnValue("Comment_Id", varId);
				varJourneyComment.SetColumnValue("Journey_Id", item.GetPrimaryKeyValue());
				varJourneyComment.Save();
			}

		}

		public static void SaveJourneyMap(int varId, System.Web.UI.WebControls.ListItemCollection itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Comment_Id=@Comment_Id", Comment.Schema.Provider.Name);
			cmdDel.AddParameter("@Comment_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (System.Web.UI.WebControls.ListItem l in itemList) 
			{
				if (l.Selected) 
				{
					JourneyComment varJourneyComment = new JourneyComment();
					varJourneyComment.SetColumnValue("Comment_Id", varId);
					varJourneyComment.SetColumnValue("Journey_Id", l.Value);
					varJourneyComment.Save();
				}

			}

		}

		public static void SaveJourneyMap(int varId , int[] itemList) 
		{
			QueryCommandCollection coll = new SubSonic.QueryCommandCollection();
			//delete out the existing
			 QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Comment_Id=@Comment_Id", Comment.Schema.Provider.Name);
			cmdDel.AddParameter("@Comment_Id", varId);
			coll.Add(cmdDel);
			DataService.ExecuteTransaction(coll);
			foreach (int item in itemList) 
			{
				JourneyComment varJourneyComment = new JourneyComment();
				varJourneyComment.SetColumnValue("Comment_Id", varId);
				varJourneyComment.SetColumnValue("Journey_Id", item);
				varJourneyComment.Save();
			}

		}

		
		public static void DeleteJourneyMap(int varId) 
		{
			QueryCommand cmdDel = new QueryCommand("DELETE FROM JourneyComment WHERE Comment_Id=@Comment_Id", Comment.Schema.Provider.Name);
			cmdDel.AddParameter("@Comment_Id", varId);
			DataService.ExecuteQuery(cmdDel);
		}

		
		#endregion
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varId,string varTitle,string varCommentText,int varUserId,int? varReplyToCommentId,DateTime varCommentDate,decimal varRate)
		{
			Comment item = new Comment();
			
			item.Id = varId;
			
			item.Title = varTitle;
			
			item.CommentText = varCommentText;
			
			item.UserId = varUserId;
			
			item.ReplyToCommentId = varReplyToCommentId;
			
			item.CommentDate = varCommentDate;
			
			item.Rate = varRate;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,string varTitle,string varCommentText,int varUserId,int? varReplyToCommentId,DateTime varCommentDate,decimal varRate)
		{
			Comment item = new Comment();
			
				item.Id = varId;
				
				item.Title = varTitle;
				
				item.CommentText = varCommentText;
				
				item.UserId = varUserId;
				
				item.ReplyToCommentId = varReplyToCommentId;
				
				item.CommentDate = varCommentDate;
				
				item.Rate = varRate;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"Id";
			 public static string Title = @"Title";
			 public static string CommentText = @"CommentText";
			 public static string UserId = @"User_Id";
			 public static string ReplyToCommentId = @"ReplyToComment_Id";
			 public static string CommentDate = @"CommentDate";
			 public static string Rate = @"Rate";
						
		}

		#endregion
	}

}

